import java.lang.*;
import java.util.*;

public class TypeB extends Thread 
{
	private static Semaphore G4, G1, G3;
	private static Storage St;
	private static DataCollection Data;
	private static System currentTime;
	private static Random randomB = new Random(2);

	private static void MoveToG4()
	{
		double g4Time = 1100-200+2*200*randomB.nextFloat(); /* 1.1+-0.2 hour, define 1hour = 1000ms */
		try
		{
			sleep((long)g4Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG1()
	{
		double g1Time = 800-200+2*200*randomB.nextFloat(); /* 0.8+-0.2 hour */
		try
		{
			sleep((long)g1Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	
	private static void MoveToG3()
	{
		double g3Time = 500-200+2*200*randomB.nextFloat(); /* 0.5+-0.2 hour */
		try
		{
			sleep((long)g3Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	
	public TypeB(Semaphore group4, Semaphore group1, Semaphore group3, Storage myStorage, DataCollection d)
	{
		G4 = group4;
		G1 = group1;
		G3 = group3;
		St = myStorage;
		Data = d;
		setDaemon(true);
	}
	
	public void run()
	{
			
		long tBeg, tG40, tG41, tG10, tG11, tG30, tG31, tStW0, tStW1, tEnd;
		
		tBeg = currentTime.currentTimeMillis(); 
		
		//move to G4
		tG40 = currentTime.currentTimeMillis(); 
		if(G4.getValue()<=0)
		{
			Data.getG4WQ();
		}	   		
		G4.take();
		tG41 = currentTime.currentTimeMillis();
		Data.getG4WT(tG41-tG40); //get G4 waiting time  		
  		
  		MoveToG4();
  		G4.release();  		
  		  
  		
  		//move to G1
		tG10 = currentTime.currentTimeMillis(); 
		if(G1.getValue()<=0)
		{
			Data.getG1WQ();
		}	   	   			
		G1.take();
		tG11 = currentTime.currentTimeMillis();
		Data.getG1WT(tG11-tG10); //get G1 waiting time
		 		      		
  		MoveToG1();
  		G1.release();  		
  	
  		//get intermediate product from Storage
		tStW0 = currentTime.currentTimeMillis();
		if(St.getValue()<=0)
		{
			Data.getStWQ();
		}	   	
		St.take();    			
		tStW1 = currentTime.currentTimeMillis();
		Data.getStWT(tStW1-tStW0);//get wait G5 waiting time	
  		     		
  		
  		//move to G3
		tG30 = currentTime.currentTimeMillis(); 
		if(G3.getValue()<=0)
		{
			Data.getG3WQ();
		}	   	
		G3.take();    			
		tG31 = currentTime.currentTimeMillis();
		Data.getG3WT(tG31-tG30);//get G3 waiting time	
  		     		
  		MoveToG3();      		
  		G3.release(); 
  		
  	
  		
  		//type2 residence time and throughput
  		tEnd = currentTime.currentTimeMillis();  		
  		Data.getBR(tEnd - tBeg); 
  		
      	//System.out.println("T2 Running");
    }
}
      		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	